public class HistoryTuple {
  public String url;
  public Calendar cal;  
  
  public HistoryTuple(String u, Calendar c) {
    this.url = u;
    this.cal = c; 
  }
  
  public String getURL() {
    return url; 
  }
  
  public Calendar getCal() {
    return cal; 
  }
}

public class HistoryData {
  ArrayList urlList;
  ArrayList dateList;
  ArrayList timeList;
  ArrayList ampmList;
  HashMap urlTime;
  HashMap hrMap;
  
  ArrayList[] allData;
    
  public HistoryData() {
    urlList = new ArrayList();
    dateList = new ArrayList();
    timeList = new ArrayList();
    urlTime = new HashMap();
    hrMap = new HashMap();
    
    allData = new ArrayList[75 * 24];
    
    parseHistoryFile();   
    computeFillDeltaTimes();
  }

  public HashMap getURLTimeMap() {
    return urlTime; 
  }
  
  private void parseHistoryFile() {
    File hiFile = new File(dataPath("history.txt")) ;
    BufferedReader hReader;
    
    try {
      hReader = new BufferedReader(new FileReader(hiFile));
      String hLine;
      
      while((hLine = hReader.readLine()) != null) {
        hLine = hLine.substring(7);
        String[] histBits = hLine.split("\t");
        String url = histBits[0];
        
      
        if(url.charAt(0) == '/'){
          url = url.substring(1);
        }
        
        url = url.split("/")[0];
        if (url.contains(":")) continue; // Removing files browsed from local machine (C:\)        
        urlList.add(url);
        
        Calendar cal = getGregorianCalendar(histBits[1].split(" ")[0], 
                             histBits[1].split(" ")[1], 
                             histBits[1].split(" ")[2]);
        
        dateList.add(cal);
        timeList.add(cal.getTime().getTime() / 1000);
  
      }
    }
    catch(Exception e) {
      e.printStackTrace(); 
    }
  }
  
  public Calendar getCalendarDay(int dayValue) {
    if(dayValue == -1)
      return (Calendar)dateList.get(dateList.size() - 1); 
    return (Calendar)dateList.get(dayValue); 
  }
  
  private GregorianCalendar getGregorianCalendar(String date, String time, String ampm) {
    String[] dateBits = date.split("/");
    String[] timeBits = time.split(":");
    
    int hr = Integer.parseInt(timeBits[0]);
    if(ampm.matches("PM"))
      hr += 12;
      
    GregorianCalendar gc = new GregorianCalendar(Integer.parseInt(dateBits[2]), 
                                                 Integer.parseInt(dateBits[0]), 
                                                 Integer.parseInt(dateBits[1]), 
                                                 hr, 
                                                 Integer.parseInt(timeBits[1]), 
                                                 Integer.parseInt(timeBits[2]));
    
    return gc;
  }
  
  private void computeFillDeltaTimes() {
    //println(timeList.size());
    final Long startDeficit = (Long)timeList.get(timeList.size() - 1);
    
    for(int i = 0; i < timeList.size(); i++) {
      Long secSinceStart = (Long)timeList.get(i) - startDeficit;
      int hrsSinceStart = secSinceStart.intValue() / 3600;
      allData[hrsSinceStart] = new ArrayList();
      allData[hrsSinceStart].add(new HistoryTuple((String)urlList.get(i), (Calendar)dateList.get(i)));
    }
  }
  
  public ArrayList getHourData(int hr) {
    if (hr < 0 )
      return null;
    return allData[hr]; 
  }
  
  int getNoOfObservedDays() {
    Calendar day0 = (Calendar)dateList.get(dateList.size() - 1);
    Calendar dayLast = (Calendar)dateList.get(0);
    
    return (dayLast.get(Calendar.DAY_OF_YEAR) - day0.get(Calendar.DAY_OF_YEAR) + 1); 
  }
}
